/*********************************/
/** MOS 6502.ts author hansong  **/
/*********************************/

//内存构造器
class Memory {
    data: Uint8Array;

    //读内存
    read8(address: number): number {
        return this.data[address];
    }
    //写内存
    write8(address: number, value: number): void {
        let temp = new Uint8Array(1);
        temp[0] = value;
        this.data[address] = temp[0];
    }

    //读内存
    read16(address: number): number {
        return this.data[address] << 8 + this.data[address + 1];
    }
    //写内存
    write16(address: number, value: number): void {
        this.data[address + 1] = value && 0xff;
        this.data[address] = value >> 8 && 0xff;
    }


    //8bit压栈
    push8(value: number): void {

    }

    //8bit出栈
    pop8(value: number): void {

    }
    //16bit压栈
    push16(value: number): void {

    }
    //16bit出栈
    pop16(value: number): void {

    }

    //初始化内存
    constructor(maxMemory: number) {
        this.data = new Uint8Array(maxMemory);
    }
}





let memory: Memory = new Memory(64 * 1024);


memory.write8(222, 23);

console.log(memory.read8(222))

/********** Debug tools **********/
// Debug mode, disabled by default
let debug_mode = false;

// Trace mode (step-by-step execution), disabled by default
let trace_mode = false
// R8[0] = AC
// R8[1] = Y
// R8[2] = X
// R8[3] = ST

let R8 = new Uint8Array(4);
// R16[0] = SP
// R16[1] = PC
let R16 = new Uint16Array(2);



console.log(b(2, 0, 1));
// x(): write a number in hexadecimal
// @param n: the number
// @param i (optional): the length of the hexadecimal value, with leading zeros (max: 8)
function x(n, i) { return ((i ? 1e7 : "") + n.toString(16).toUpperCase()).slice(-i) }



/********** Binary tools **********/

// lshift()
// @params a, b
// @return a << b
function lshift(number, shift) { return number * Math.pow(2, shift) }

// rshift()
// @params a, b
// @return a >> b
function rshift(number, shift) { return Math.floor(number / Math.pow(2, shift)) }

// b()
// @params number, start, end
// @return a number representing the bits of number read from start to end
// end param is optional. By default, one bit is read.
function b(number, start, end) { return rshift(number, start) & (Math.pow(2, (end || start) - start + 1) - 1) }

// ror()
// @params number, length, bits
// @return the number (which is "length" bits long) after a right rotation (make the "bits" first bits of the number, last) 
function ror(number, length, bits) { return lshift((number & Math.pow(2, bits) - 1), length - bits) + rshift(number, bits) }


function reset() {
    this.R16[0] = 0xff;
    this.R16[1] = 0x600;
}


